﻿using MxWeiXinPF.Common;
using MxWeiXinPF.Web.admin.sms;
using MxWeiXinPF.WeiXinComm;
using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using Senparc.Weixin.MP.CommonAPIs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.shop
{
    public partial class addprescription : MxWeiXinPF.Web.UI.UserPage
    {
        public string carturl = "";
        private MxWeiXinPF.BLL.wx_dt_user_BaseData daladd = new MxWeiXinPF.BLL.wx_dt_user_BaseData();
        private int id = 0;
        public string indexurl = "";
        private int pid = 0;
        public string purl = "";
        protected MxWeiXinPF.Model.users umodel;
        public string userinfourl = "";
        public string categoryurl = "";
        private WeiXinSendMessageComm sm = new WeiXinSendMessageComm();
        private BlueSmsMgr msgsm = null;
        protected void btnHome_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Concat(new object[] { "detail.aspx?wid=", base.wid, "&openid=", base.openid, "&pid=", this.pid }));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {


            if (base.openid.Equals(""))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"请在微信中打开页面\");</script>");
            }
            else if (this.DoEdit())
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"处方上传成功\");</script>");
                base.Response.Redirect(string.Concat(new object[] { "detail.aspx?wid=", base.wid, "&openid=", base.openid, "&pid=", this.pid }));
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"处方上传过程中发生错误\");</script>");
            }
        }

        private bool DoEdit()
        {
            string str = Utils.DropHTML(this.txturl.Value);
            string text = this.txtRemarks.Text;
            string str3 = "";
            if (text.Length > 15)
            {
                str3 = text.Substring(0, 15);
            }
            else
            {
                str3 = text;
            }
            bool flag = false;
            MxWeiXinPF.BLL.addprescription addprescription = new MxWeiXinPF.BLL.addprescription();
            MxWeiXinPF.Model.addprescription model = new MxWeiXinPF.Model.addprescription();
            if (this.umodel == null)
            {
                this.umodel = this.daladd.GetModelByOpenID(base.openid);
            }
            model.wid = base.wid;
            model.openid = base.openid;
            model.userid = this.umodel.id;
            model.shopid = this.umodel.shopid;
            model.pid = this.pid;
            model.url = str;
            model.title = str3;
            model.remarks = text;
            model.status = "0";
            if (addprescription.Add(model) > 0)
            {
                flag = true;
                //上传成功，发送短信或者微信消息通知店长
                if (msgsm == null)
                {
                    msgsm = new BlueSmsMgr(39);
                }
                Model.manager mman = new BLL.manager().GetModel(umodel.shopid);
                if (mman != null)
                {

                    if (!string.IsNullOrEmpty(mman.shopclerkid))
                    {
                        //如果指定了微信店长,则发送模板消息
                        if (!SendTemplateMsg("",model,umodel,mman))
                        {//发送模版消息失败
                            SendSMMsg("", model, umodel, mman);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(mman.telephone))
                        {
                            SendSMMsg("", model, umodel, mman);
                        }
                    }
                }
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                base.wid = MyCommFun.RequestInt("wid");
                base.openid = MyCommFun.RequestOpenid();
                this.pid = MyCommFun.RequestInt("pid");
                this.shopid = MyCommFun.RequestInt("shopid");
                if (!this.Page.IsPostBack)
                {
                    if (this.umodel == null)
                    {
                        this.umodel = this.daladd.GetModelByOpenID(base.openid);
                    }
                    this.purl = string.Concat(new object[] { "prescription.aspx?wid=", base.wid, "&openid=", base.openid, "&pid=", this.pid });
                    this.indexurl = string.Concat(new object[] { "index.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                    this.carturl = string.Concat(new object[] { "cart.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                    this.userinfourl = string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                    this.categoryurl = MyCommFun.urlAddOpenid("/shop/category.aspx?wid=" + base.wid + "&shopid=" + shopid, base.openid);
                }
            }
            catch (Exception ex)
            {

            }

        }

        private bool SendTemplateMsg(string content, MxWeiXinPF.Model.addprescription model, Model.users umodel, Model.manager mmodel)
        {
            try
            {
                content = string.Concat("你好,你的店铺:", mmodel.real_name, "的客户:", umodel.nick_name, "提交了一张处方,请尽快审核");
                Model.wx_userweixin weixin = new MxWeiXinPF.BLL.wx_userweixin().GetModel(39);
                if (weixin == null)
                {
                    return false;
                }
                if (!AccessTokenContainer.CheckRegistered(weixin.AppId))
                {
                    AccessTokenContainer.Register(weixin.AppId, weixin.AppSecret);
                }

                WxJsonResult ret = Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendText(weixin.AppId, mmodel.shopclerkid, content);
                if (ret.errcode == 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                
            }
            if (mmodel == null)
            {
                return false;
            }


            return false;
        }

        private bool SendSMMsg(string content,MxWeiXinPF.Model.addprescription model,Model.users umodel,Model.manager mmodel)
        {
            if (mmodel == null)
            {
                return false;
            } 
            BlueSmsMgr sm = new BlueSmsMgr(39);
            content = string.Concat("你好,你的店铺:", mmodel.user_name, "的客户:", umodel.nick_name, "提交了一张处方,请尽快审核");
            sm.SendSMS(mmodel.telephone, content);
            return false;
        }
    }
}